﻿using Sidvall.Data;
using System.ComponentModel;

namespace Sidvall.IO
{
    public sealed class File : IItem, INotifyPropertyChanged
    {
        #region Public Members

        public event PropertyChangedEventHandler PropertyChanged;

        #region CreationTime

        private System.DateTime? _CreationTime;
        public System.DateTime? CreationTime
        {
            get
            {
                return _CreationTime;
            }
            set
            {
                if (_CreationTime == value)
                    return;
                _CreationTime = value;
                OnPropertyChanged(nameof(CreationTime));
            }
        }

        #endregion
        #region FullName

        private string _FullName;
        public string FullName
        {
            get
            {
                return _FullName;
            }
            set
            {
                if (_FullName == value)
                    return;
                _FullName = value;
                OnPropertyChanged(nameof(FullName));
            }
        }

        #endregion
        #region LastWriteTime

        private System.DateTime? _LastWriteTime;
        public System.DateTime? LastWriteTime
        {
            get
            {
                return _LastWriteTime;
            }
            set
            {
                if (_LastWriteTime == value)
                    return;
                _LastWriteTime = value;
                OnPropertyChanged(nameof(LastWriteTime));
            }
        }

        #endregion
        #region DisplayName

        private string _DisplayName;
        public string DisplayName
        {
            get
            {
                return _DisplayName;
            }
            set
            {
                if (_DisplayName == value)
                    return;
                _DisplayName = value;
                OnPropertyChanged(nameof(DisplayName));
            }
        }

        #endregion
        #region Name

        private string _Name;
        public string Name
        {
            get
            {
                return _Name;
            }
            set
            {
                if (_Name == value)
                    return;
                _Name = value;
                OnPropertyChanged(nameof(Name));
            }
        }

        #endregion
        #region NameWithoutExtension

        public string NameWithoutExtension
        {
            get
            {
                if (string.IsNullOrWhiteSpace(this.Name))
                    return string.Empty;
                return System.IO.Path.GetFileNameWithoutExtension(this.Name);
            }
        }

        #endregion
        #region Size

        private long? _Size;
        public long? Size
        {
            get
            {
                return _Size;
            }
            set
            {
                if (_Size == value)
                    return;
                _Size = value;
                OnPropertyChanged(nameof(Size));
            }
        }

        #endregion
        #region StorageName

        private string _StorageName;
        public string StorageName
        {
            get
            {
                return _StorageName;
            }
            set
            {
                if (_StorageName == value)
                    return;
                _StorageName = value;
                OnPropertyChanged(nameof(StorageName));
            }
        }

        #endregion

        #region Copy

        public File Copy()
        {
            return new File()
            {
                _CreationTime = this.CreationTime,
                _FullName = this.FullName,
                _LastWriteTime = this.LastWriteTime,
                _Name = this.Name,
                _Size = this.Size,
                _StorageName = this.StorageName,
            };
        }

        #endregion
        #region ToString

        public override string ToString()
        {
            return this.FullName;
        }

        #endregion

        #region IItem Members

        string IItem.Id
        {
            get
            {
                return this.FullName;
            }
        }
        string IItem.Name
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(this.Name))
                    return this.Name;
                return this.FullName;
            }
        }

        #endregion

        #endregion
        #region Private Members

        #region OnPropertyChanged

        private void OnPropertyChanged(string propertyName)
        {
            OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }
        private void OnPropertyChanged(PropertyChangedEventArgs args)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged.Invoke(this, args);
        }

        #endregion

        #endregion
        #region Constructors

        public File()
        {
        }

        #endregion
    }
}
